package Thread_Safety_Issue;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:34398
 * Date:2024-07-17
 * Time:10:53
 */
public class demo1 {
    public static int count = 0;
    public static Object object1 = new Object();
    public static Object object2 = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                synchronized (object1) {//第一次锁了
                    synchronized(object1){//第二次我在锁？
                        count++;
                    }
                }

            }
        });


        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                synchronized (object2) {
                    count++;
                }
            }
        });
        //分别通过两个线程对count++
        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println("count=" + count);
    }
}
